package com.example.brain_wave.ui.theme

import android.app.Activity
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowCompat

// 科技感深色主题
private val TechDarkColorScheme = darkColorScheme(
    primary = TechBlue,
    onPrimary = TechBackground,
    primaryContainer = TechBlueDark,
    onPrimaryContainer = TechTextPrimary,
    
    secondary = TechCyan,
    onSecondary = TechBackground,
    secondaryContainer = TechCyanDark,
    onSecondaryContainer = TechTextPrimary,
    
    tertiary = TechBlueLight,
    onTertiary = TechBackground,
    tertiaryContainer = TechSurfaceVariant,
    onTertiaryContainer = TechTextPrimary,
    
    background = TechBackground,
    onBackground = TechTextPrimary,
    
    surface = TechSurface,
    onSurface = TechTextPrimary,
    surfaceVariant = TechSurfaceVariant,
    onSurfaceVariant = TechTextSecondary,
    
    error = TechError,
    onError = TechTextPrimary,
    errorContainer = Color(0xFF2D1B1B),
    onErrorContainer = TechError,
    
    outline = TechBorder,
    outlineVariant = TechDivider,
    
    inverseSurface = TechTextPrimary,
    inverseOnSurface = TechBackground,
    inversePrimary = TechBlueDark
)

private val DarkColorScheme = darkColorScheme(
    primary = Purple80,
    secondary = PurpleGrey80,
    tertiary = Pink80
)

private val LightColorScheme = lightColorScheme(
    primary = Purple40,
    secondary = PurpleGrey40,
    tertiary = Pink40
)

@Composable
fun Brain_waveTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // Dynamic color is available on Android 12+
    dynamicColor: Boolean = false, // 禁用动态颜色，使用我们的科技主题
    content: @Composable () -> Unit
) {
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }
        // 默认使用科技感主题
        darkTheme -> TechDarkColorScheme
        else -> TechDarkColorScheme // 即使在浅色模式也使用深色科技主题
    }
    
    val view = LocalView.current
    if (!view.isInEditMode) {
        SideEffect {
            val window = (view.context as Activity).window
            window.statusBarColor = colorScheme.background.toArgb()
            WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = false
        }
    }

    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography,
        content = content
    )
}